package springboot;

import com.aizuda.easy.retry.client.starter.EnableEasyRetry;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import springboot.component.IService;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

@SpringBootApplication
@EnableAspectJAutoProxy
@MapperScan(basePackages = {"springboot.mapper"})
public class LearnJavaApplication {

    public static void main(String[] args) {
        SpringApplication.run(LearnJavaApplication.class, args);
    }

    @Component
    static class DefaultLoadBeanList implements CommandLineRunner {

        @Autowired
        private ApplicationContext context;

        @Autowired
        private ConfigurableApplicationContext configurableApplicationContext;

        @Resource(name = "otherServiceImpl")
        private IService service;

        @Autowired
        private ObjectMapper objectMapper;

        @PostConstruct
        public void start() {
            System.out.println("===before create bean");
        }

        @PreDestroy
        public void stop() {
            System.out.println("===after destroy bean");
        }

        @Override
        public void run(String... args) throws Exception {
            // 向JVM注册Spring关闭钩子，JVM关闭时，关闭Spring容器
            configurableApplicationContext.registerShutdownHook();
            System.out.println("===Spring Boot===启动时加载了[" + context.getBeanDefinitionCount() + "]个bean对象");
            System.out.println("objectMapper=" + objectMapper.getClass());
            service.say();
//            String[] beanNames = context.getBeanDefinitionNames();
//            Arrays.sort(beanNames);
//            System.out.println("===Spring Boot===启动加载Bean: ");
//            for (String name : beanNames) {
//                System.out.println(name);
//            }
        }
    }
}
